using System;
using System.Configuration;
using System.Data;
using digit.Core;

namespace SMS.MediaManager
{
	public class TransInfoManager
	{
		#region CONSTRUCTORS
		static TransInfoManager()
		{
			_data = new TransInfoData(ConfigurationManager.ConnectionStrings["SMS"].ConnectionString);
			_error = "";
		}
		#endregion
		
		#region ATTRIBUTES
		private static TransInfoData _data;
		private static string _error;
		#endregion
		
		#region PROPERTIES
		public static string ErrorMessage
		{
			get
			{
				return _error;
			}
		}
		
		#endregion
		
		#region METHODS
        public static bool Insert(DateTime transDate, string userID, long dataID, string dataName, string dataType, int sizeId)
		{
			_error = "";
			try
			{
				object[] values = new object[6];
				values[0] = transDate;
				values[1] = Util.ReplaceNullTo(userID, DBNull.Value);
				values[2] = Util.ReplaceNullTo(dataID, DBNull.Value);
                values[3] = Util.ReplaceNullTo(dataName, DBNull.Value);
				values[4] = Util.ReplaceNullTo(dataType, DBNull.Value);
				values[5] = Util.ReplaceNullTo(sizeId, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Insert( TransInfoInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[6];
				values[0] = info.TransDate;
				values[1] = Util.ReplaceNullTo(info.UserID, DBNull.Value);
				values[2] = Util.ReplaceNullTo(info.DataID, DBNull.Value);
                values[3] = Util.ReplaceNullTo(info.DataName, DBNull.Value);
				values[4] = Util.ReplaceNullTo(info.DataType, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.SizeID, DBNull.Value);
		
				return _data.Insert(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}

        public static bool Update(long transID, DateTime transDate, string userID, long dataID, string dataName, string dataType, int sizeId)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = transID;
				values[1] = transDate;
				values[2] = Util.ReplaceNullTo(userID, DBNull.Value);
				values[3] = Util.ReplaceNullTo(dataID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(dataName, DBNull.Value);
				values[5] = Util.ReplaceNullTo(dataType, DBNull.Value);
                values[6] = Util.ReplaceNullTo(sizeId, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Update( TransInfoInfo info)
		{
			_error = "";
			try
			{
				object[] values = new object[7];
				values[0] = info.TransID;
				values[1] = info.TransDate;
				values[2] = Util.ReplaceNullTo(info.UserID, DBNull.Value);
				values[3] = Util.ReplaceNullTo(info.DataID, DBNull.Value);
                values[4] = Util.ReplaceNullTo(info.DataName, DBNull.Value);
				values[5] = Util.ReplaceNullTo(info.DataType, DBNull.Value);
				values[6] = Util.ReplaceNullTo(info.SizeID, DBNull.Value);
		
				return _data.Update(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( long transID)
		{
			_error = "";
			try
			{
				object[] values = { transID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static bool Delete( TransInfoInfo info)
		{
			_error = "";
			try
			{
				object[] values = { info.TransID };
				return _data.Delete(values);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return false;
			}
		}
		
		public static TransInfoInfo Select( long transID)
		{
			_error = "";
			try
			{
				object[] values = { transID };
				DataTable dTable = _data.Select(values);
				if (dTable.Rows.Count < 1) return null;
		
				
				TransInfoInfo info = new TransInfoInfo(dTable.Rows[0]);
				return info;
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}

        public static DataTable SelectInfo(string userId, long transId)
        {
            _error = "";
            try
            {
                object[] values = new object[2];
                values[0] = userId;
                values[1] = transId;
                return _data.SelectInfo(values);
            }
            catch (Exception ex)
            {
                _error = ex.Message;
                return null;
            }
        }
		
		public static DataTable SelectAll()
		{
			_error = "";
			try
			{
				return _data.SelectAll();
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder,  int pageIndex,  int pageSize)
		{
			_error = "";
			try
			{
				object[] values = { whereClause, sortOrder };
				int index = (pageIndex * pageSize) - 1;
				if (index < 0) index = 0;
				return _data.Search(values, index, pageSize);
			}
			catch(Exception ex)
			{
				_error = ex.Message;
				return null;
			}
		}
		
		public static DataTable Search( string whereClause,  string sortOrder)
		{
			return TransInfoManager.Search(whereClause, sortOrder, 0, 0);
		}
		
		public static DataTable Search( int pageIndex,  int pageSize)
		{
			return TransInfoManager.Search( "", "", pageIndex, pageSize);
		}
		
		#endregion
		
	}
}